//
//  XDReconmmendCollectionViewCell.m
//  Le way walkers
//
//  Created by POP-mac on 15/5/14.
//  Copyright (c) 2015年 LTXZ. All rights reserved.
//

#import "XDReconmmendCollectionViewCell.h"
#import "UIImageView+WebCache.h"

@interface XDReconmmendCollectionViewCell ()
@property (nonatomic, retain) UILabel *time;
@property (nonatomic,retain)UILabel *name;
@property (nonatomic, assign) CGFloat w;
@property (nonatomic, assign) CGFloat h;
@property (nonatomic, retain) UIImageView *back_view;
//@property(nonatomic,retain)UIImageView *image;
@end

@implementation XDReconmmendCollectionViewCell

- (void)dealloc
{
    [_pic release];
    [_name release];
    [_time release];
    [_back_view release];
    [super dealloc];
}
//初始化创建控件
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        self.image = [[[UIImageView alloc]init]autorelease];
//        [self.contentView addSubview:_image];
        self.time = [[[UILabel alloc] init] autorelease];
        //        self.time.backgroundColor = [UIColor blueColor];
        [self.time setFont:[UIFont systemFontOfSize:13]];
        [self.contentView addSubview:self.time];
        
        self.name = [[[UILabel alloc]init]autorelease];
        [self.name setNumberOfLines:0];
        [self.name setFont:[UIFont systemFontOfSize:13]];
        [self.name setBackgroundColor:[UIColor whiteColor]];
        [self.contentView addSubview:_name];
        
        self.pic = [[[UIImageView alloc]init]autorelease];
        self.pic.userInteractionEnabled = YES;
        [self.contentView addSubview:_pic];
        
        self.back_view = [[[UIImageView alloc] init] autorelease];
        [self.pic addSubview:self.back_view];
    }
    return self;
}
//布局信息的方法
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
//    _image.frame = CGRectMake(15, 25, self.contentView.frame.size.width-30, self.contentView.frame.size.height);
    _time.frame = CGRectMake(15, 5, 200, 15);
//    _pic.frame = CGRectMake(15, 25, self.contentView.frame.size.width - 30, 200);
//    _name.frame = CGRectMake(15, 225, _pic.frame.size.width, 180);
}
//model对象 setter方法 给控件赋值
- (void)setDetails:(XDDetails *)details
{
    self.back_view.hidden = NO;
    if (_details != details) {
        [_details release];
        _details = [details retain];
    }
    [self.pic sd_setImageWithURL:[NSURL URLWithString:details.photo] placeholderImage:[UIImage imageNamed:@"69E58PIChJC_1024.jpg"]];
    //NSLog(@"%f", self.pic.frame.size.height);
    _name.text = details.text;
    self.back_view.image = [UIImage imageNamed:@"jiantou"];
    NSString *day = [NSString stringWithFormat:@"第%@天 %@", details.day, details.local_time];
    self.time.text = day;
//    _time_1.image = [UIImage imageNamed:@"时间_1"];
    
    if (details.photo_info == nil) {
       // NSLog(@"kong");
        self.w = self.contentView.frame.size.width - 30;
        self.h = 200;
    }
    else
    {
        self.w = [[details.photo_info objectForKey:@"w"] integerValue];
        self.h = [[details.photo_info objectForKey:@"h"] integerValue];
    }
    //NSLog(@"%@", details.photo);
    if (details.photo.length <= 3) {
        self.back_view.hidden = YES;
        self.w = 1;
        self.h = 0;
    }
    _pic.frame = CGRectMake(15, 25, self.contentView.frame.size.width - 30, (self.contentView.frame.size.width - 30) / self.w * self.h);
    self.back_view.frame = CGRectMake(0, (self.contentView.frame.size.width - 30) / self.w * self.h - 20, self.contentView.frame.size.width - 30, 20);
    if ([details.text isEqualToString:@""]) {
        _name.frame = CGRectMake(15, 25 + (self.contentView.frame.size.width - 30) / self.w * self.h, _pic.frame.size.width, 0);
    }
    else
    {
        CGFloat f = [[self class] heightWithTxet:self.name.text];
        _name.frame = CGRectMake(15, 25 + (self.contentView.frame.size.width - 30) / self.w * self.h, _pic.frame.size.width, f);
    }
}

+ (CGFloat)heightWithTxet:(NSString *)text
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:13], NSFontAttributeName, nil];
    
    CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width - 30, 5000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}

@end
